<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        ref="trTable"
        requestName="getListHandle"
        isNeedSelection
        v-model="searchParams"
        :total="totalNum"
        :minSearchLabelWidth="102"
        :checkSelectable="checkSelectable"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :btnList="btnList"
        :tableColumn="tableColumn"
        :tableData="tableData"
        @view="operateHandle($event, 'view')"
        @inventory="operateHandle($event, 'inventory')"
        @export="operateHandle($event, 'export')"
        @rowInventory="operateHandle($event, 'rowInventory')"
      />
    </div>

    <TrDetail
      v-model="isShowFristPopup"
      popupTitle="查看详情"
      :leftList="fristDetail"
      statusModel="hidden"
      :width="1227"
      :label-width="fristWidth"
      :detailData="fristData"
      @close="isShowFristPopup = false"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="fristSearchParams"
          :tableData="fristTable"
          :total="fristTotal"
          :tableColumn="fristColunm"
          requestName="getDetailHandle"
          @viewDetail="operateHandle($event, 'viewDetail')"
        />
      </template>
    </TrDetail>
    <TrDetail
      v-model="isShowSecondPopup"
      popupTitle="查看详情"
      :leftList="secondDetail"
      statusModel="hidden"
      :width="1227"
      :label-width="secondWidth"
      :detailData="secondData"
      @close="isShowSecondPopup = false"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="secondSearchParams"
          :tableData="secondTable"
          :total="secondTotal"
          :tableColumn="secondColunm"
          :expandObj="secondExpandObj"
          requestName="getSecondDetailHandle"
        />
      </template>
    </TrDetail>
  </div>
</template>

<script>
import {
  getList,
  inventory,
  getDetail,
  getParticulars,
} from "@/api/logistics/inventoryReview.js";
import { getColor } from "@/utils/billColor";
import { getObject } from "./js/inventoryReview";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";

export default {
  name: "orderSearch",
  components: {
    TrTable,
    TrDetail,
  },
  computed: {
    ids() {
      let ids = [];
      this.$refs.trTable.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  data: () => ({
    btnList: [
      {
        label: "审核",
        option: "inventory",
        color: "blue",
        isRelaTable: true,
      },
      {
        label: "导出",
        option: "export",
        color: "white",
      },
    ],
    tableColumn: [
      {
        type: "text",
        label: "入库单据",
        value: "billCode",
      },
      {
        type: "text",
        label: "审核状态",
        value: "auditStateName",
      },
      {
        type: "text",
        label: "单据状态",
        value: "stateName",
      },
      {
        type: "text",
        label: "仓库业务类型",
        value: "whBizTypeName",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
      },
      {
        type: "text",
        label: "审核人员",
        value: "confirmUserName",
      },
      {
        type: "text",
        label: "审核时间",
        value: "confirmTime",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 120,
        showBtnList: [
          {
            label: "查看",
            option: "view",
          },
          {
            label: "审核",
            option: "rowInventory",
          },
        ],
      },
    ],
    searchHideList: [
      {
        id: "billWhTypeCodeList",
        type: "multiSelect",
        width: 33.3,
        label: "仓库业务类型",
        value: [],
        list: [],
      },
      {
        id: "billCode",
        type: "input",
        width: 33.3,
        label: "入库单据",
        value: "",
      },

      {
        id: "createUserName",
        type: "input",
        width: 33.3,
        label: "创建人员",
        value: "",
      },
      {
        id: "confirmUserName",
        type: "input",
        width: 33.3,
        label: "审核人员",
        value: "",
      },
      {
        id: "auditStateList",
        type: "multiSelect",
        width: 33.3,
        label: "审核状态",
        list: [],
        value: [],
      },
      // {
      //   id: "stateList",
      //   type: "multiSelect",
      //   width: 33.3,
      //   label: "单据状态",
      //   list: [],
      //   value: [],
      // },
      {
        startStr: "confirmTimeStart",
        endStr: "confirmTimeEnd",
        id: "createdTime1",
        type: "daterange",
        width: 80,
        label: "审核时间",
        value: [],
        timePeriod: true,
        radio: -1,
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createTimeStart",
        endStr: "createTimeEnd",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        timePeriod: true,
        radio: 1,
      },
    ],
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,
    isShowFristPopup: false,
    fristTotal: 0,
    fristColunm: [],
    fristData: {},
    fristTable: [],
    fristDetail: [],
    fristSearchParams: {
      current: 1,
      size: 10,
    },
    isShowSecondPopup: false,
    secondTotal: 0,
    secondColunm: [],
    secondData: {},
    secondTable: [],
    secondDetail: [],
    secondSearchParams: {
      current: 1,
      size: 10,
      bizDetailId: "",
    },
    fristWidth: 72,
    secondWidth: 72,
    secondExpandObj: {},
  }),
  mounted() {
    // this.getListHandle();
    this.initHandle();
  },
  methods: {
    checkSelectable(row) {
      return row.auditStateName == "待审核";
    },
    async initHandle() {
      const res = await this.$fetchEnumApi("mes-warehouse-service", [
        "EntryAuditAuditStateEnum",
        "EntryAuditBillStateEnum",
        "BizBillWarehouseBusinessTypeEnum",
      ]);
      const audCol = this.$findByProperty(
        this.searchHideList,
        "id",
        "auditStateList"
      );
      this.$set(audCol, "list", res["EntryAuditAuditStateEnum"]);
      // const entCol = this.$findByProperty(
      //   this.searchHideList,
      //   "id",
      //   "stateList"
      // );
      // this.$set(entCol, "list", res["EntryAuditBillStateEnum"]);

      const bizCol = this.$findByProperty(
        this.searchHideList,
        "id",
        "billWhTypeCodeList"
      );
      this.$set(bizCol, "list", res["BizBillWarehouseBusinessTypeEnum"]);
    },
    inventoryHandle(params) {
      this.$textShow({
        title: this.$t("tip.label"),
        content: [this.$t("tip.inventoryTip")],
      })
        .then(async () => {
          let res = await inventory(params);
          if (res.data.code != 200) return this.$message.error(res.data.msg);
          this.$message.success(this.$t("msg.success"));
          this.$refs.trTable.clearSelect();
          this.getListHandle();
        })
        .catch(() => {});
    },
    operateHandle(val, type) {
      switch (type) {
        case "view": {
          const valueObj = getObject(val.whBizTypeCode);
          if (!valueObj) return this.$message.warning("暂未找到该单据类型!");
          this.fristSearchParams.bizBillId = val.id;
          this.secondSearchParams.billType = val.whBizTypeCode;
          this.fristColunm = valueObj.firstTableColumn;
          this.fristDetail = valueObj.firstDetailList;
          this.fristWidth = valueObj.firstWidth;
          this.secondWidth = valueObj.secondWidth;
          this.getDetailHandle();
          break;
        }

        case "inventory":
          this.inventoryHandle(this.ids);
          break;
        case "rowInventory":
          this.inventoryHandle([val.id]);
          break;
        case "viewDetail": {
          const valueObj = getObject(this.secondSearchParams.billType);
          if (!valueObj) return this.$message.warning("暂未找到该单据类型!");
          this.secondData = val;
          this.secondDetail = valueObj.secondDetailList;
          this.secondColunm = valueObj.secondTableColumn;
          this.secondSearchParams.bizDetailId = val.bizDetailId;
          this.secondExpandObj =
            this.secondSearchParams.billType == "CR" ? valueObj.expandObj : {};
          this.getSecondDetailHandle();
          break;
        }
        case "export":
          this.$exportHandle({ vm: this, params: this.searchParams });
          break;
      }
    },
    async getDetailHandle() {
      const { data } = await getDetail(this.fristSearchParams);
      if (data.code !== 200) return;
      this.$validateData(data.data);
      this.fristData = data.data;
      this.fristTable = data.data.pageResp.records;
      this.fristTable.forEach((item) => {
        item.newPurcNumUnit = `${item.purcNumUnitCode}(${item.purcNumUnitName})`;
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
      });
      this.fristTotal = data.data.pageResp.total;
      const auditItem = this.$findByProperty(
        this.fristDetail,
        "id",
        "auditStateName",
        "list"
      );
      const stateItem = this.$findByProperty(
        this.fristDetail,
        "id",
        "stateName",
        "list"
      );
      if (auditItem) auditItem.valueColor = getColor("DF", data.data.state);
      if (stateItem) stateItem.valueColor = getColor("DF", data.data.state);
      this.isShowFristPopup = true;
    },
    async getSecondDetailHandle() {
      const { data } = await getParticulars(this.secondSearchParams);
      if (data.code !== 200) return;
      this.$validateData(data.data);
      this.secondData = Object.assign({}, this.secondData, data.data);
      this.secondTable = data.data.records;
      this.secondTable.forEach((item) => {
        item.newPurcNumUnit = `${item.purcNumUnitCode}(${item.purcNumUnitName})`;
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
        if (item.labelContainerList) {
          item.labelContainerList.forEach((val) => {
            val.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
            val.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
          });
        }
      });
      this.secondTotal = data.data.total;
      this.isShowSecondPopup = true;
    },
    async getListHandle() {
      const { data } = await getList(this.searchParams);
      if (data.code != 200) return this.$message.error(data.msg);
      this.$validateData(data.data);
      this.tableData = data.data.records;
      this.totalNum = data.data.total;
      this.tableData.forEach((item) => {
        item["auditStateNameColor"] = getColor("DF", item.state);
        item["stateNameColor"] = getColor("DF", item.state);
        item.isShowrowInventory = item.auditStateName == "已审核";
      });
    },
  },
};
</script>
